﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Foothill.Utils;
using GGFW.Entity.SystemManage;
using GGFW.DataAccess.SystemManage;

namespace GGFW.BusinessLogic.SystemManage
{
    public class FoothillMenuControl : IDisposable
    {
        /// <summary>
        /// 根据条件获取功能菜单 
        /// </summary>
        /// <param name="userType"></param>
        /// <returns></returns>
        public FoothillOperationResult<FoothillMenu> GetFunctions(int userID)
        {
            FoothillOperationResult<FoothillMenu> result = new FoothillOperationResult<FoothillMenu>();
            result.Success = true;
            try
            {
                using (FoothillMenuDao dao = new FoothillMenuDao())
                {
                    result.Data = dao.GetFunctions(userID);
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
                // result.PromptMessage = Message.OTHERERROR;
            }
            return result;
        }



        /// <summary>
        /// 根据条件获取功能菜单 
        /// </summary>
        public List<FoothillMenu> GetUserFunctions(int UserID)
        {
            using (FoothillMenuDao dao = new FoothillMenuDao())
            {
                return dao.GetFunctions(UserID);
            }

            return null;
        }



        /// <summary>
        /// 获得菜单树形结构列表，未封装返回结果
        /// </summary>
        /// <returns>实体列表</returns>
        public List<ExtTreeNode> GetMenuTree_Naked()
        {
            FoothillMenuDao dal = new FoothillMenuDao();
            List<FoothillMenu> orgs = dal.GetAll();

            List<ExtTreeNode> result = new List<ExtTreeNode>();
            ExtTreeNode root = new ExtTreeNode("0", "菜单", false);
            generateChildren(root, orgs);

            result = root.children;
            return result;
        }
        /// <summary>
        /// 初始化部门树节点的字节点列表
        /// </summary>
        /// <param name="parent">父节点</param>
        /// <param name="datas">数据来源</param>
        private void generateChildren(ExtTreeNode parent, List<FoothillMenu> datas)
        {
            if (datas == null) return;
            var queryresult = from FoothillMenu data in datas
                              where data.ParentMenuID == parent.id
                              select data;

            List<FoothillMenu> childList = queryresult.ToList<FoothillMenu>();

            //是否叶子节点
            if (childList.Count == 0)
            {
                parent.leaf = true;
                return;
            }

            if (parent.children == null)
                parent.children = new List<ExtTreeNode>();
            foreach (FoothillMenu org in childList)
            {
                ExtTreeNode node = new ExtTreeNode(org.MenuID.ToString(), org.Text, false);
                generateChildren(node, datas);
                parent.children.Add(node);
            }
        }

        /// <summary>
        /// 获得菜单权限树形结构列表，未封装返回结果
        /// </summary>
        /// <returns>实体列表</returns>
        public List<ExtTreeNode> GetFunctionsTree()
        {
            FoothillMenuDao dal = new FoothillMenuDao();
            List<FoothillMenu> menus = dal.GetAll();
            // List<FoothillMenu> rights = new FunctionControl().GetRights().Data;

            List<ExtTreeNode> result = new List<ExtTreeNode>();
            ExtTreeNode root = new ExtTreeNode("0", "所有菜单", false);
            List<string> iDList = new List<string>();
            root.children = new List<ExtTreeNode>();
            foreach (FoothillMenu func in menus)
            {
                if (!iDList.Contains(func.MenuID) && func.IsLeaf == "0")
                {
                    iDList.Add(func.MenuID);
                    ExtTreeNode node = new ExtTreeNode(func.MenuID, func.Text, false);
                    root.children.Add(node);
                    GenerateChildren(node, menus);
                }
            }

            result = root.children;
            return result;
        }

        /// <summary>
        /// 生成子结点数据集
        /// </summary>
        /// <param name="node">结点</param>
        /// <param name="menus">菜单集</param>
        /// <param name="rights">功能集</param>
        private void GenerateChildren(ExtTreeNode node, List<FoothillMenu> menus)
        {
            List<string> iDList = new List<string>();
            foreach (FoothillMenu func in menus)
            {
                if (!iDList.Contains(func.MenuID) && func.ParentMenuID == node.id)
                {
                    iDList.Add(func.MenuID);
                    #region 修改暂时不考虑功能权限处理
                    //ExtTreeNode childNode = new ExtTreeNode(func.FuncID, func.FuncName, false);
                    #endregion
                    ExtTreeNode childNode = new ExtTreeNode(func.MenuID, func.Text, true);
                    if (node.children == null)
                        node.children = new List<ExtTreeNode>();
                    node.children.Add(childNode);
                    #region 修改暂时不考虑功能权限处理
                    //GenerateGrandChildren(childNode, rights);
                    #endregion
                }
            }
        }

        /// <summary>
        /// 生成孙结点数据集
        /// </summary>
        /// <param name="node">子结点</param>
        /// <param name="datas">功能集</param>
        private void GenerateGrandChildren(ExtTreeNode node, List<FoothillMenu> datas)
        {
            string menuID = node.id.Substring(0, 3);
            var queryresult = from FoothillMenu data in datas
                              where data.MenuID.Substring(0, 3) == menuID
                              select data;

            List<FoothillMenu> childList = queryresult.ToList<FoothillMenu>();

            //是否叶子节点
            if (childList.Count == 0)
            {
                node.leaf = true;
                return;
            }

            if (node.children == null)
                node.children = new List<ExtTreeNode>();
            foreach (FoothillMenu func in childList)
            {
                ExtTreeNode grandChildNode = new ExtTreeNode(func.MenuID, func.Text, true);
                node.children.Add(grandChildNode);
            }
        }

        /// <summary>
        /// 根据条件获取功能菜单 
        /// </summary>
        /// <param name="userType"></param>
        /// <returns></returns>
        public FoothillOperationResult<FoothillMenu> GetFoothillMenuByRoleID(int roleID)
        {
            FoothillOperationResult<FoothillMenu> result = new FoothillOperationResult<FoothillMenu>();
            result.Success = true;
            try
            {
                using (FoothillMenuDao dao = new FoothillMenuDao())
                {
                    result.Data = dao.GetFoothillMenuByRoleID(roleID);
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
                // result.PromptMessage = Message.OTHERERROR;
            }
            return result;
        }

        /// <summary>
        /// 根据条件获取功能菜单 
        /// </summary>
        /// <param name="userType"></param>
        /// <returns></returns>
        public FoothillOperationResult<FoothillMenu> GetFoothillMenuByUserID(int userID)
        {
            FoothillOperationResult<FoothillMenu> result = new FoothillOperationResult<FoothillMenu>();
            result.Success = true;
            try
            {
                using (FoothillMenuDao dao = new FoothillMenuDao())
                {
                    result.Data = dao.GetFoothillMenuByUserID(userID);
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
                // result.PromptMessage = Message.OTHERERROR;
            }
            return result;
        }

        /// <summary>
        /// 设置角色菜单
        /// </summary>
        /// <param name="MenuID">菜单ID</param>
        /// <param name="RoleIDs">角色ID</param>
        /// <returns>受影响的记录数</returns>
        public FoothillOperationResult<Foothill.Privilege.Entity.FoothillRoleMenu> SetMenuRole(string MenuIDs, string RoleID)
        {
            FoothillOperationResult<Foothill.Privilege.Entity.FoothillRoleMenu> result = new FoothillOperationResult<Foothill.Privilege.Entity.FoothillRoleMenu>();
            try
            {
                using (FoothillMenuDao dao = new FoothillMenuDao())
                {
                    //返回结果
                    result.AffectedRows = dao.SetMenuRole(MenuIDs, RoleID);
                    result.Success = true;
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 设置用户菜单
        /// </summary>
        /// <param name="MenuID">菜单ID</param>
        /// <param name="RoleIDs">角色ID</param>
        /// <returns>受影响的记录数</returns>
        public FoothillOperationResult<Foothill.Privilege.Entity.FoothillUserMenu> SetMenuUser(string MenuIDs, string UserID)
        {
            FoothillOperationResult<Foothill.Privilege.Entity.FoothillUserMenu> result = new FoothillOperationResult<Foothill.Privilege.Entity.FoothillUserMenu>();
            try
            {
                using (FoothillMenuDao dao = new FoothillMenuDao())
                {
                    //返回结果
                    result.AffectedRows = dao.SetMenuUser(MenuIDs, UserID);
                    result.Success = true;
                }
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.Message = ex.Message;
            }

            return result;
        }

        #region IDisposable 成员

        public void Dispose()
        {
        }

        #endregion
    }
}
